//
//  ViewController.m
//  PickerViewForCities
//
//  Created by 移云 on 2019/11/11.
//  Copyright © 2019 MaoYeYeDeJiangCuCha. All rights reserved.
//

#import "ViewController.h"
#import "ProvincesModel.h"
@interface ViewController ()<UIPickerViewDelegate,UIPickerViewDataSource>

@property(nonatomic,strong)NSArray* provincesArray;

@property(nonatomic,strong)UILabel* provinceLabel;
@property(nonatomic,strong)UILabel* cityLabel;
@property(nonatomic,strong)UIPickerView* provincePickerView;

@end

@implementation ViewController

#pragma mark - 懒加载
- (NSArray *)provincesArray
{
    if (_provincesArray == nil)
    {
        //加载字典数组
        NSArray *dictArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"02cities.plist" ofType:nil]];
        NSMutableArray *arrayM = [NSMutableArray arrayWithCapacity:dictArray.count];
        for (NSDictionary *dict in dictArray)
        {
            ProvincesModel *model = [ProvincesModel provincesWithDict:dict];
            [arrayM addObject:model];
        }
         _provincesArray = arrayM;
    }
    return _provincesArray;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor lightGrayColor];
    [self setupUI];
    //默认选中
    [self pickerView:self.provincePickerView didSelectRow:0 inComponent:0]; 
}




-(void)setupUI
{
    
    UIPickerView *provincePickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 20, UIScreen.mainScreen.bounds.size.width, 200)];
    [self.view addSubview:provincePickerView];
    self.provincePickerView = provincePickerView;
    self.provincePickerView.backgroundColor =  [UIColor orangeColor];
    provincePickerView.delegate = self;
    provincePickerView.dataSource = self;
    
    UILabel *provinceLabel = [[UILabel alloc] init];
    self.provinceLabel = provinceLabel;
    [self.view addSubview:provinceLabel];
    [provinceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.top.equalTo(provincePickerView.mas_bottom).offset(20);
    }];
    
    UILabel *cityLabel = [[UILabel alloc] init];
    self.cityLabel = cityLabel;
    [self.view addSubview:cityLabel];
    [cityLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(provinceLabel.mas_left);;
        make.top.equalTo(provinceLabel.mas_bottom).offset(20);
    }];
    
    
}


#pragma mark - UIPickerViewDataSource
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (component == 0)
    {
        ProvincesModel *privince = self.provincesArray[row];
        return privince.name;
    }
    
    NSInteger selIndex = [pickerView selectedRowInComponent:0];
    ProvincesModel *province = self.provincesArray[selIndex];
    
    if (row < province.cities.count){ //防止数组越界
        return province.cities[row];
    }
    return province.cities[0];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
        if (component == 0){
            ProvincesModel *model = self.provincesArray[row];
            self.provinceLabel.text = model.name;
            [pickerView reloadComponent:1];
            [pickerView selectRow:0 inComponent:1 animated:true];
            self.cityLabel.text = model.cities[0];
        }else{
           
            NSInteger selIndex = [pickerView selectedRowInComponent:0];
            ProvincesModel *province = self.provincesArray[selIndex];
            if (row < province.cities.count){ //防止数组越界
                self.cityLabel.text = province.cities[row];
            }
        }
}



#pragma mark - UIPickerViewDelegate
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (component == 0) //省份
    {
        return self.provincesArray.count;
    }
    
    //城市
    //省份选中的行
    NSInteger selIndex = [pickerView selectedRowInComponent:0];
    ProvincesModel *province = self.provincesArray[selIndex];
    return province.cities.count;
}

@end
